package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.Random;

public class RegisterPage extends Utils {
    public static String url = "http://8.156.68.126:8080/login.html";

    public RegisterPage() {
        super(url);
    }

    // 生成 1-9999 的随机数
    Random random = new Random();
    int randomNumber1 = random.nextInt(9999) + 1; // 范围: 1-9999
    int randomNumber2 = random.nextInt(9999) + 1; // 范围: 1-9999

    //检查页面是否可以正常打开
    public void checkPageRight() throws InterruptedException {
        //等待弹幕加载完毕
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.Andu")));
        Thread.sleep(300);

        //检查模态窗元素是否加载成功
        driver.findElement(By.cssSelector("body > div.login-container"));

        //检查登录框
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    //异常注册
    public void RegisterFail() {
        //从登录页进入注册
        driver.findElement(By.cssSelector("body > div.login-container > div.login-footer > a")).click();

        //1.用户已经存在
        driver.findElement(By.cssSelector("#username")).sendKeys("Rubens");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#surepassword")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        // 定位弹幕（通过类名和文本内容）
        WebElement Toast01 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.error")));

        // 验证弹窗内容
        assert Toast01.getText().equals("注册失败");

        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#surepassword")).clear();

        //2.两次输入的密码不一致

        // 关键点：等待第一个弹窗完全消失（通过元素不可见或移除）
        wait.until(ExpectedConditions.invisibilityOf(Toast01));

        driver.findElement(By.cssSelector("#username")).sendKeys("AutoTest" + randomNumber1 + "_" + randomNumber2);
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#surepassword")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        // 定位弹幕（通过类名和文本内容）
        WebElement Toast02 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.error")));

        // 验证弹窗内容
        assert Toast02.getText().equals("密码不一致");

        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#surepassword")).clear();

        //3.密码为空
        // 关键点：等待第一个弹窗完全消失（通过元素不可见或移除）
        wait.until(ExpectedConditions.invisibilityOf(Toast02));

        driver.findElement(By.cssSelector("#username")).sendKeys("AutoTest" + randomNumber1 + "_" + randomNumber2);
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#surepassword")).sendKeys("");
        driver.findElement(By.cssSelector("#submit")).click();

        // 定位弹幕（通过类名和文本内容）
        WebElement Toast03 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.error")));

        // 验证弹窗内容
        assert Toast03.getText().equals("密码不能为空");
    }

    //成功注册
    public void RegisterSuc() {
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#surepassword")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("AutoTest" + randomNumber1 + "_" + randomNumber2);
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#surepassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //注册成功会返回登录---h1
        wait.until(ExpectedConditions.urlContains("login"));
    }
}
